
import java.util.concurrent.atomic.AtomicInteger;

public class Demo2 {
    private int value;
    public boolean CAS(int M, int A, int B){
        //getAndIncrement();
        return true;
    }
    public int getAndDecrement() {
        int oldValue = value;
        while (!CAS(value, oldValue, oldValue-1)) {
            oldValue = value;
        }
        return oldValue;
    }
    public static AtomicInteger cnt = new AtomicInteger();
    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                cnt.getAndIncrement();//后置++
                //cnt.getAndDecrement();后置--
                //cnt.decrementAndGet();前置--
                //cnt.incrementAndGet();前置++
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                cnt.getAndIncrement();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("cnt = " + cnt.get());
    }
}
